﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using LifeTree.DomainModel.Repositories;
using AutoMapper;
using LifeTree.DomainModel.Entities;
using LifeTree.Mvc.ViewModels;
using LifeTree.Core.DataAccess;
using System.ComponentModel.DataAnnotations;
using System.Web;
using LifeTree.Mvc.ActionResults;

namespace LifeTree.Mvc.Controllers
{
    public class WorkbookController : BaseController
    {
        public WorkbookController(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }

        //[AcceptVerbs(HttpVerbs.Post)]
        //public ActionResult Create(string name)
        //{
        //    var workBook = unitOfWork.WorkbookRepository.GetByName(name);
        //    if (workBook == null)
        //    {
        //        workBook = entityFactory.CreateWorkBook(name);
        //        if (!TryValidateModel(workBook))
        //        {
        //            return View();
        //        }

        //        unitOfWork.WorkbookRepository.Add(workBook);
        //        unitOfWork.SaveChanges();
        //    }
        //    return Redirect(Url.RouteUrl("WorkBook", new { workBookName = name }));
        //}

        //[AcceptVerbs(HttpVerbs.Get)]
        //public ActionResult Item(string workBookName)
        //{
        //    var workBook = unitOfWork.WorkbookRepository.GetByName(workBookName);
        //    if (workBook == null)
        //    {
        //        workBook = entityFactory.CreateWorkBook(workBookName);
        //        unitOfWork.WorkbookRepository.Add(workBook);
        //        unitOfWork.SaveChanges();
        //    }
        //    var viewModel = Mapper.Map<Workbook, WorkBookViewModel>(workBook);
        //    return View(viewModel);
        //}

        [HttpGet]
        public ActionResult Create()
        {
            return View("Create");
        }

        [HttpPost]
        public ActionResult Create(WorkbookInputViewModel inputModel)
        {
            Workbook workbook = entityFactory.CreateWorkBook(inputModel.Name);

            TryValidateModel(workbook);

            if (unitOfWork.WorkbookRepository.GetByName(inputModel.Name) != null)
                ModelState.AddModelError("Name", "Workbook with the same name already exist");

            if (!ModelState.IsValid)
                return View("Create", inputModel);

            unitOfWork.WorkbookRepository.Add(workbook);
            unitOfWork.SaveChanges();

            return RedirectToRoute("WorkbookDetails", new { workbookName = inputModel.Name });
        }

        [HttpGet]
        public ActionResult Details(string workbookName)
        {
            Workbook workbook = unitOfWork.WorkbookRepository.GetByName(workbookName);
            if (workbook == null)
                return NotFoundResult();

            return View("Details", Mapper.Map<Workbook, WorkBookViewModel>(workbook));
        }

        [HttpGet]
        public JsonResult IsWorkbookNameAvailable(string name)
        {
            object data;

            if (unitOfWork.WorkbookRepository.GetByName(name) == null)
                data = true;
            else
                data = String.Format("Workbook with spedified name already exist. You can try to <a href='/{0}'>open it</a> or choose another name.", name);

            return Json(data, JsonRequestBehavior.AllowGet);
        }
    }
}
